package com.hnkywl.sts.service.sfgl;

import com.hnkywl.sts.dao.jhgl.JkdDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.organ.NbgsDao;
import com.hnkywl.sts.dao.sfgl.FkshmxDao;
import com.hnkywl.sts.dao.sfgl.TpfkDao;
import com.hnkywl.sts.dao.zjgl.PjglDao;
import com.hnkywl.sts.entity.jhgl.Jkd;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sfgl.Sfkmx;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Xyz;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.jhgl.JkdService;
import com.hnkywl.sts.service.organ.KhhService;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.PjglService;
import com.hnkywl.sts.service.zjgl.XyzService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sfgl
 * 类描述:   特批付款服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class TpfkService {
    private BmService bmService;
    private JkdDao jkdDao;
    private TpfkDao tpfkDao;
    private NbgsDao nbgsDao;
    private FkshmxDao fkshmxDao;
    private NbgsService nbgsService;
    private KhhService khhService;
    private PjglService pjglService;
    private JkdService jkdService;
    private XyzService xyzService;
    private GsjgDao gsjgDao;
    private FlowService flowService;
    private PjglDao pjglDao;


    /**
     * 查询特批付款列表
     *
     * @param
     * @param sbgsId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllTpfk(String bm, String sbgsMc, Long sbgsId,String gldwMc,String zt, String zdr,int pageNo, int pageSize) {
        Long nbgsIds = null;
        if (sbgsId != null && sbgsMc.equals(gsjgDao.get(sbgsId).getJc())) {
            nbgsIds = sbgsId;
        }
        Page<Sfk> sfkPage = this.tpfkDao.findTpfkList(bm, sbgsMc, nbgsIds, gldwMc,zt,zdr, pageNo, pageSize);
        List<Sfk> sfkList = sfkPage.getResult() == null ? new ArrayList<Sfk>() : sfkPage.getResult();
        String sfkJson = BaseUtils.fetchCollectionToJson(sfkList, "id", "bm", "sfkrq", "sfklx", "gldw.qc", "pjlx.z", "sfje", "kxlx", "shrq", "sbgs.jc", "sbr.xm", "bz", "zt","processInstanceId", "createUser.id", "createUser.xm");
        return "{\"total\":" + sfkPage.getTotalCount() + ",\"rows\":[" + sfkJson + "]}";
    }

    @Transactional
    public String findTpfkByCghtList( Long cghtId, int pageNo, int pageSize) {
        List<Jkd> jkds = this.jkdService.findJkdDetailByCght(cghtId);
        List<Sfk> sfkList = new ArrayList<Sfk>();
        Integer count = 0;
        if (jkds.size() > 0) {
            for (Jkd jkd : jkds) {
                List<Sfkmx> sfkmxLst = jkd.getSfkmxList();
                if (sfkmxLst != null) {
                    for (Sfkmx sfkmx : sfkmxLst) {
                        Page<Sfk> sfkPage = this.tpfkDao.findTpfkBySfkList(sfkmx.getFksh().getId(), pageNo, pageSize);
                        sfkList.addAll(sfkPage.getResult());
                        count += sfkPage.getTotalCount();
                    }
                }
            }
        }
        String sfkJson = BaseUtils.fetchCollectionToJson(sfkList, "id", "bm","sfkrq", "sfklx", "gldw.jc", "pjlx.z", "sfje", "sbgs.jc", "sbr.xm", "bz", "zt", "createUser.id", "createUser.xm");
        return "{\"total\":" + count + ",\"rows\":[" + sfkJson + "]}";
    }

    /**
     * 首页显示付款单数
     *
     * @return
     */
    public String findAllTpfk() {
        Integer tpfkNum = 0;
        try {
            Page<Sfk> sfkPage = this.tpfkDao.findTpfkList(null,null,null,null,"","", 1, -1);
            tpfkNum = sfkPage.getTotalCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tpfkNum.toString();
    }

    //获取付款单号
    public String getFkdbm() {
        return bmService.getSfdbm("FK");
    }

    /**
     * 保存特批付款信息
     *
     * @param sfk 特批付款实体
     * @return
     */
    @Transactional
    public void saveTpfk(Sfk sfk,String sfje) {
        nullId(sfk);
        sfk.setSfje(Double.parseDouble(sfje.replace(",","").replace("，","")));
        sfk.setCreateUser(YgService.getCurrentYg());
        sfk.setCreateOrgan(YgService.getCurrentYg().getZggs());
        sfk.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        sfk.setDateCreated(new Date());
        sfk.setSfkrq(sfk.getDateCreated());
        sfk.setLastUpdated(sfk.getDateCreated());
        sfk.setSftpfy("tpfk");
        sfk.setWlzlx(0);
        sfk.setBm(this.bmService.getSfdbm("FK"));
        sfk.setZt("ybc");
        sfk.setSfwsyfk(0);
        this.tpfkDao.save(sfk);
    }

    /**
     * 更新特批付款信息
     *
     * @param sfk 特批付款实体
     * @return
     */
    @Transactional
    public void updateTpfk(Sfk sfk,String sfje) {
        Sfk clone = this.findTpfkDetail(sfk.getId());
        nullId(sfk);
        clone = (Sfk) ReflectionUtils.getInstance(clone, sfk);
        clone.setLastUpdated(new Date());
        clone.setSfkrq(clone.getLastUpdated());
        clone.setSfje(Double.parseDouble(sfje.replace(",","").replace("，","")));
        clone.setSftpfy("tpfk");
        this.tpfkDao.save(clone);
    }

    @Transactional
    public void TpfkTj(String ids, String status) {
        String[] fkIds = ids.split(",");
        for(String id :fkIds){
            Sfk sfk = this.findTpfkDetail(Long.parseLong(id));
            if (this.flowService.checkFlowStart("特批付款流程")) {
                String processInstanceId = this.flowService.regTask(sfk.getProcessInstanceId(), "特批付款流程", StringUtils.EMPTY);
                sfk.setProcessInstanceId(processInstanceId);
                sfk.setZt("ytj");
            }else{
                sfk.setZt("ysh");
            }
            sfk.setLastUpdated(new Date());
            this.tpfkDao.save(sfk);
        }
    }


    @Transactional
    public void TpfkQxsh(Long id, String status) {
        Sfk sfk = this.findTpfkDetail(id);
        sfk.setZt(status);
        sfk.setLastUpdated(new Date());
        this.tpfkDao.save(sfk);
    }

    @Transactional
    public void TpfkTh(Long id, String status) {
        Sfk sfk = this.findTpfkDetail(id);
        sfk.setZt(status);
        sfk.setLastUpdated(new Date());
        this.tpfkDao.save(sfk);
    }

    /**
     * 删除特批付款信息
     *
     * @param id 特批付款id
     * @return
     */
    @Transactional
    public void deleteTpfk(Long id) {
        Sfk sfk = tpfkDao.get(id);
        sfk.setLastUpdated(new Date());
        sfk.setDeleted(1);
        this.tpfkDao.save(sfk);
        Double yfje = 0d;
        if (sfk.getSfkmxList() != null) {
            for (Sfkmx temp : sfk.getSfkmxList()) {
                Sfkmx x = fkshmxDao.get(temp.getId());
                x.setFksh(sfk);
                yfje = Double.parseDouble(x.getJkd().getYfje().toString()) - Double.parseDouble(temp.getBcfkmx().toString());
                Jkd j = jkdDao.get(x.getJkd().getId());
                j.setYfje(yfje);
                x.setJkd(j);
                x.setBcfkmx(Double.parseDouble(temp.getBcfkmx().toString()));
                x.setLastUpdated(new Date());
                fkshmxDao.save(x);
            }
        }
        if (sfk.getSfkmxList() != null) {
            for (Sfkmx t : sfk.getSfkmxList()) {
                Sfkmx m = fkshmxDao.get(t.getId());
                m.setFksh(null);
                m.setLastUpdated(new Date());
                fkshmxDao.save(m);
            }
        }
    }

    /**
     * 查询特批付款明细
     *
     * @param id
     * @return
     */
    @Transactional
    public Sfk findTpfkDetail(Long id) {
        return this.tpfkDao.getTpfk(id);
    }

    /**
     * 查询特批付款明细
     *
     * @param processInstanceId 收付款ID
     * @return
     */
    @Transactional
    public Sfk findTpfkDetailByProcessInstance(String processInstanceId) {
        return this.tpfkDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    /**
     * 特批付款操作流程
     *
     * @param tpfkId
     * @return
     */
    @Transactional
    public void checkTpfkPj(Long tpfkId, String pjIds) {
        Sfk sfk = this.findTpfkDetail(tpfkId);
        List<Pj> rzpjList = pjglDao.getPjByFkId(sfk.getId());
        for(Pj p:rzpjList){
            p.setStatus(1);
            pjglDao.save(p);
        }
        sfk.setLastUpdated(new Date());
        sfk.setZt("yrz");
        sfk.setShrq(new Date());
        sfk.setShr(YgService.getCurrentYg());
        this.tpfkDao.save(sfk);
        String[] pjs = pjIds.split(",");
        for (String id : pjs) {
            Pj pj = pjglService.findPjDetail(Long.parseLong(id));
            pj.setSpr(sfk.getGldw());
            pj.setStatus(5);
            pj.setSyrq(new Date());
            pj.setLastUpdated(new Date());
            if(sfk.getSfklx() ==1 || sfk.getSfklx()==2){
                pj.setSfk(sfk);
            }else{
                pj.setSk(sfk);
            }
            pjglService.savePjgl(pj);
        }
    }

    /**
     * 特批付款操作流程
     *
     * @param tpfkId
     * @return
     */
    @Transactional
    public void checkTpfk(Long tpfkId, Long glkhhId) {
        Sfk sfk = this.findTpfkDetail(tpfkId);
        sfk.setNbkhh(khhService.findKhhById(glkhhId));
        sfk.setLastUpdated(new Date());
        sfk.setZt("yrz");
        sfk.setShrq(new Date());
        sfk.setShr(YgService.getCurrentYg());
        this.tpfkDao.save(sfk);
    }

    /**
     * 审核信用证
     * @param fkshId
     * @param xyzIds
     */
    @Transactional
    public void checkTpfkXyz(Long fkshId, String xyzIds) {
        Sfk sfk = this.findTpfkDetail(fkshId);
        sfk.setLastUpdated(new Date());
        sfk.setZt("yrz");
        sfk.setShrq(new Date());
        sfk.setShr(YgService.getCurrentYg());
        this.tpfkDao.save(sfk);
        String[] xyzs = xyzIds.split(",");
        for (String id : xyzs) {
            Xyz xyz = this.xyzService.findXyzDetail(Long.parseLong(id));
            xyz.setJzdw(sfk.getGldw());
            xyz.setStatus(4);
            xyz.setLastUpdated(new Date());
            xyz.setSyrq(new Date());
            xyz.setSfk(sfk);
            xyz.setSfdrlx("wdr");
            this.xyzService.saveOldXyz(xyz);
        }
    }


    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, Object field) {
        return tpfkDao.checkUnique(id, value, field);
    }

    @Transactional
    public void changeStatus(String ids, String status) {
        String[] fkIds = ids.split(",");
        for(String id :fkIds){
            Sfk sfk = this.findTpfkDetail(Long.parseLong(id));
            sfk.setZt(status);
            this.tpfkDao.save(sfk);
        }
//        if (status == "ytj") {
//            Double yfje = 0d;
//            if (sfk.getSfkmxList() != null) {
//                for (Sfkmx temp : sfk.getSfkmxList()) {
//                    Sfkmx x = fkshmxDao.get(temp.getId());
//                    x.setFksh(sfk);
//                    yfje = Double.parseDouble(x.getJkd().getYfje().toString()) + Double.parseDouble(temp.getBcfkmx().toString());
//                    Jkd j = jkdDao.get(x.getJkd().getId());
//                    j.setYfje(yfje);
//                    x.setJkd(j);
//                    x.setBcfkmx(Double.parseDouble(temp.getBcfkmx().toString()));
//                    x.setLastUpdated(new Date());
//                    fkshmxDao.save(x);
//                }
//            }
//        }

    }


    /**
     * 关联判断
     *
     * @param sfk
     */
    private void nullId(Sfk sfk) {
        if (sfk != null && sfk.getPjlx() != null && sfk.getPjlx().getId() == null)
            sfk.setPjlx(null);
        if (sfk != null && sfk.getGldw() != null && sfk.getGldw().getId() == null)
            sfk.setGldw(null);
        if (sfk != null && sfk.getHt() != null && sfk.getHt().getId() == null)
            sfk.setHt(null);
        if (sfk != null && sfk.getHklx() != null && sfk.getHklx().getId() == null)
            sfk.setHklx(null);
        if (sfk != null && sfk.getSbgs() != null && sfk.getSbgs().getId() == null)
            sfk.setSbgs(null);
        if (sfk != null && sfk.getSbbm() != null && sfk.getSbbm().getId() == null)
            sfk.setSbbm(null);
        if (sfk != null && sfk.getSbr() != null && sfk.getSbr().getId() == null)
            sfk.setSbr(null);
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setTpfkDao(TpfkDao tpfkDao) {
        this.tpfkDao = tpfkDao;
    }

    @Autowired
    public void setNbgsDao(NbgsDao nbgsDao) {
        this.nbgsDao = nbgsDao;
    }

    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }

    @Autowired
    public void setKhhService(KhhService khhService) {
        this.khhService = khhService;
    }

    @Autowired
    public void setPjglService(PjglService pjglService) {
        this.pjglService = pjglService;
    }

    @Autowired
    public void setJkdDao(JkdDao jkdDao) {
        this.jkdDao = jkdDao;
    }

    @Autowired
    public void setFkshmxDao(FkshmxDao fkshmxDao) {
        this.fkshmxDao = fkshmxDao;
    }
    @Autowired
    public void setJkdService(JkdService jkdService) {
        this.jkdService = jkdService;
    }
    @Autowired
    public void setXyzService(XyzService xyzService) {
        this.xyzService = xyzService;
    }

    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }
    @Autowired
    public void setPjglDao(PjglDao pjglDao) {
        this.pjglDao = pjglDao;
    }
}
